package com.tangmu.rabbits.ui.fragment.home;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.adapter.home.HomeGoodsAdapter;
import com.tangmu.rabbits.adapter.home.HomeGridAdapter;
import com.tangmu.rabbits.base.BaseOneFragment;
import com.tangmu.rabbits.ui.activity.home.BrandIflashbuyActivity;
import com.tangmu.rabbits.ui.activity.home.NineElementActivity;
import com.tangmu.rabbits.ui.activity.home.TodayChooseActivity;
import com.tangmu.rabbits.ui.activity.mine.MessageActivity;
import com.tangmu.rabbits.ui.activity.mine.SignInActivity;
import com.tangmu.rabbits.ui.fragment.BlankFragment;
import com.tangmu.rabbits.ui.fragment.home.goods.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页
 */
public class HomeStickyFragment extends BaseOneFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {

    public static final String TAG = "HomeStickyFragment";
    /*   @BindView(R.id.home_titleBar)
       LinearLayout llTitle;*/
    @BindView(R.id.home_app_bar)
    AppBarLayout appBarLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.menu_gridView)
    GridView gridView;
    /**
     * 筛选Tabs
     */
    @BindView(R.id.optimization_tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.tv_goods_all)
    TextView tvAll;
    @BindView(R.id.tv_goods_sales_volume)
    TextView tvSales;
    @BindView(R.id.tv_goods_newest)
    TextView tvNewest;
    @BindView(R.id.tv_goods_price)
    TextView tvPrice;

    @BindView(R.id.vp_home)
    ViewPager mViewPager;

    private List<Integer> datas = new ArrayList<>();
    private HomeGridAdapter gridAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabs = {"全部", "女装", "男装", "内衣", "美妆", "居家", "母婴", "食品", "鞋包"};

  /*  @BindView(R.id.home_goods_recyclerView)
    RecyclerView mRecyclerView;
    private HomeGoodsAdapter adapter;
    private List<Integer> goods = new ArrayList<>();*/

    public HomeStickyFragment() {
        // Required empty public constructor
    }

   /* @Override
    protected boolean isImmersionBarEnabled() {
        return false;
    }*/

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.transparent).titleBar(R.id.toolbar).init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        datas.add(R.drawable.icon_today_biqiang);
        datas.add(R.drawable.icon_nine_to_nine);
        datas.add(R.drawable.icon_brand_iflash);
        datas.add(R.drawable.icon_yuan20);

        /**
         * 今日活动
         */
        gridAdapter = new HomeGridAdapter(mActivity, datas);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    startActivity(TodayChooseActivity.class);
                } else if (position == 1) {
                    startActivity(NineElementActivity.class);
                } else if (position == 2) {
                    startActivity(BrandIflashbuyActivity.class);
                } else if (position == 3) {
                    startActivity(NineElementActivity.class);
                }
            }
        });

        // appBarLayout
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.i("TAG", "verticalOffset===" + verticalOffset);
                gridView.setBackgroundColor(ContextCompat.getColor(mActivity, R.color.white));
                toolbar.setBackgroundColor(changeAlpha(getResources().getColor(R.color.colorAccent), Math.abs((verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange())));
            }
        });

        /**
         * 商品列表
         */
        for (int i = 0; i < tabs.length; i++) {
            fragments.add(new GoodsFragment());
        }
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.addOnPageChangeListener(this);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        initTabsClick();
    }

    private void initTabsClick() {
        tvAll.setOnClickListener(this);
        tvSales.setOnClickListener(this);
        tvNewest.setOnClickListener(this);
        tvPrice.setOnClickListener(this);
    }

    @OnClick({R.id.iv_home_sign, R.id.iv_home_message})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_home_sign:
                startActivity(SignInActivity.class);
                break;
            case R.id.iv_home_message:
                startActivity(MessageActivity.class);
                break;
        }
    }

    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (v == tvAll) {
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvSales) {
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvNewest) {
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvPrice) {
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        }
    }
}
